<?php

namespace magein\think\utils\api;

use magein\utils\Result;
use think\Response;

/**
 * @user magein
 * @date 2023/11/30 17:19
 */
class ApiResponse
{
    /**
     * @param Result|array|string|int $result
     * @param array $header
     * @return Response
     */
    public static function result($result, array $header = []): Response
    {
        if (!$result instanceof Result) {
            if ($result === false) {
                $result = Result::error();
            } else {
                $result = Result::success($result);
            }
        }

        return Response::create($result->toArray(), 'json')->header($header);
    }

    /**
     * @param $message
     * @param int $code
     * @param $data
     * @return Response
     */
    public static function error($message, int $code = 1, $data = null): Response
    {
        return self::result(Result::error($message, $code, $data));
    }

    /**
     * @param $data
     * @param string $message
     * @param int $code
     * @return Response
     */
    public static function success($data = null, string $message = '', int $code = 0): Response
    {
        return self::result(Result::success($data, $message, $code));
    }
}